package org.testtools.handler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.testtools.formatter.StringFormatter;

public class FileHandler {
	private static final String EMPTY = "";

	private static final String BACKSLASH = "\\";

	private static final String SLASH = "/";

	private static final char CHAR_LINE_BREAK = '\n';

	public static String getPlainXMLEscContent(final String filename)
			throws IOException {
		return StringFormatter.encodeXMLTags(getPlainXMLContent(filename));
	}

	public static String getPlainXMLContent(final String filename)
			throws IOException {
		return StringFormatter.trimXMLTags(getFileContent(filename, false));
	}

	// TODO JUnit
	public static String getFormattedFileContentIfExists(final String filename)
			throws IOException {
		if (new File(filename).exists())
			return StringFormatter.htmlWithTabulation(getFileContent(filename));
		else
			return EMPTY;
	}

	public static String getFormattedFileContent(final String filename)
			throws IOException {
		return StringFormatter.htmlWithTabulation(getFileContent(filename));
	}

	public static String getFileContent(final String filename)
			throws IOException {
		return getFileContent(filename, true);
	}

	public synchronized static String getFileContent(final String filename,
			boolean linebreak) throws IOException {
		final StringBuilder sb = new StringBuilder();

		final BufferedReader reader = new BufferedReader(new FileReader(
				filename));
		String str = null;

		while ((str = reader.readLine()) != null) {
			sb.append(str);

			if (linebreak)
				sb.append(CHAR_LINE_BREAK);
		}

		reader.close();
		return sb.toString();
	}

	public synchronized static boolean outputToFile(String fullpath, String text) {
		return outputToFile(fullpath, text, false);
	}

	private static int getSlashPosition(String fullpath) {
		return fullpath.lastIndexOf(SLASH) > fullpath.lastIndexOf(BACKSLASH) ? fullpath
				.lastIndexOf(SLASH) : fullpath.lastIndexOf(BACKSLASH);
	}

	public synchronized static boolean outputToFile(String filename,
			String text, boolean append) {
		if (filename == null || filename.isEmpty()) {
			return false;
		}

		final File file = new File(filename);

		try {
			createDirIfNotExists(filename, file);

			final BufferedWriter writer = new BufferedWriter(new FileWriter(
					file));
			if (append)
				writer.append(text);
			else
				writer.write(text);

			writer.close();

			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	private synchronized static void createDirIfNotExists(String filename,
			final File file) {
		final int pos = getSlashPosition(filename);

		if (pos > -1) {
			final File fileDir = new File(filename.substring(0, pos));

			if (file.exists())
				file.delete();
			else if (!fileDir.exists())
				fileDir.mkdirs();
		}
	}

	// TODO JUnit
	public synchronized static boolean deleteFileIfExists(String filename) {
		final File file = new File(filename);

		if (!file.exists())
			return true;
		else if (file.delete())
			return true;

		return false;
	}

	// TODO JUnit
	public synchronized static boolean deleteFile(String filename) {
		final File file = new File(filename);

		if (file.exists() && file.delete())
			return true;

		return false;
	}

	// TODO JUnit
	public synchronized static String getFilledFileContent(String filename,
			Map<String, String> values) throws IOException {
		return StringHandler.replaceAllValues(getFileContent(filename), values);
	}
}
